package com.txd.mofa.fabu.aty;

import android.Manifest;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kongzue.baseframework.interfaces.Layout;
import com.kongzue.baseframework.util.JumpParameter;
import com.kongzue.baseframework.util.OnPermissionResponseListener;
import com.kongzue.baseframework.util.Preferences;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.ninetaildemonfox.zdl.ninetails.map.JSONUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.txd.mofa.R;
import com.txd.mofa.adapter.FabuLishiAdapter;
import com.txd.mofa.base.BaseAty;
import com.txd.mofa.net.NetUtils;
import com.txd.mofa.view.ShanchuDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Layout(R.layout.aty_fabu)
public class FabuAty extends BaseAty {

    private ImageView iv_back;
    private LinearLayout ll_zipai,ll_xiangce;
    private TextView tv_shanchu;
    private RecyclerView recy;
    private FabuLishiAdapter fabuLishiAdapter;
    private int page = 1;
    private SmartRefreshLayout refreshLayout;
    private ArrayList<Map<String, String>> data;
    private LinearLayout ll_net;

    @Override
    public void initViews() {
        super.initViews();
        ll_net = findViewById(R.id.ll_net);
        refreshLayout = findViewById(R.id.refreshLayout);
        iv_back = findViewById(R.id.iv_back);
        ll_zipai = findViewById(R.id.ll_zipai);
        ll_xiangce = findViewById(R.id.ll_xiangce);
         tv_shanchu = findViewById(R.id.tv_shanchu);
        recy = findViewById(R.id.recy);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(me,4);
        recy.setLayoutManager(gridLayoutManager);

        fabuLishiAdapter = new FabuLishiAdapter(R.layout.item_fabu_lishi);
        recy.setAdapter(fabuLishiAdapter);
        fabuLishiAdapter.setEmptyView(getLayoutInflater().inflate(R.layout.text_null, null));


    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {
        ll_net.setVisibility(View.GONE);
        refreshLayout.setVisibility(View.VISIBLE);
        http();
    }

    @Override
    protected void onNetworkDisConnected() {

        Log.e("===========flag",flag+"");
        if (flag == false) {
            ll_net.setVisibility(View.VISIBLE);
            refreshLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void initDatas(JumpParameter paramer) {

    }

    @Override
    protected void onResume() {
        super.onResume();
        Huoqu();
        http();
    }

    private void http() {
        if (flag == false) {
            ll_net.setVisibility(View.VISIBLE);
            refreshLayout.setVisibility(View.GONE);
        }
        HttpRequest.POST(me, historyImgList, new Parameter().add("token",token)
                .add("page",page), new ResponseListener() {
            @Override
            public void onResponse(String response, Exception error) {
                if (error == null) {
                    refreshLayout.finishLoadMore();
                    refreshLayout.finishRefresh();
                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                    if (map.get("code").equals("1")){
                        data = JSONUtils.parseKeyAndValueToMapList(map.get("data"));

                        if (data.size() !=0) {
                            Preferences.getInstance().set(me, "face_img", "face_img", data.get(0).get("img"));
                        }

                        if (page == 1){
                            fabuLishiAdapter.setNewData(data);
                        }else {
                            if (data == null){
                                return;
                            }
                            fabuLishiAdapter.addData(data);
                        }

                    }
                }else {
                    toast("网络异常");
                }
            }
        });
    }

    @Override
    public void setEvents() {
        ll_net.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flag){
                    ll_net.setVisibility(View.GONE);
                    refreshLayout.setVisibility(View.VISIBLE);
                    http();
                }
            }
        });

        tv_shanchu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShanchuDialog shanchuDialog = new ShanchuDialog(me, "是否确定删除", new ShanchuDialog.SignListener() {
                    @Override
                    public void shanchu() {
                        HttpRequest.POST(me, delHistoryImg, new Parameter().add("token",token).add("type","2"), new ResponseListener() {
                            @Override
                            public void onResponse(String response, Exception error) {
                                if (error == null) {
                                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                                    if (map.get("code").equals("1")){
                                        http();
                                        Preferences.getInstance().set(me,"face_img","face_img","");
                                    }
                                }else {
                                    toast("网络异常");
                                }
                            }
                        });
                    }
                });
                shanchuDialog.show();
            }
        });

        fabuLishiAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                HttpRequest.POST(me, chooseImg, new Parameter().add("token",token)
                        .add("id",fabuLishiAdapter.getData().get(position).get("id")), new ResponseListener() {
                    @Override
                    public void onResponse(String response, Exception error) {
                        if (error == null) {
                            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                            if (map.get("code").equals("1")){

                                page = 1;
                                http();
                            }
                        }else {
                            toast("网络异常");
                        }
                    }
                });
            }
        });

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                http();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                http();
            }
        });

        ll_xiangce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestPermission(new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, new OnPermissionResponseListener() {
                    @Override
                    public void onSuccess(String[] permissions) {
                        jump(XiangceAty.class);
                    }

                    @Override
                    public void onFail() {

                    }
                });
            }
        });

        ll_zipai.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                requestPermission(new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, new OnPermissionResponseListener() {
                    @Override
                    public void onSuccess(String[] permissions) {
                        jump(PaizhaoAty1.class);
                    }

                    @Override
                    public void onFail() {

                    }
                });

            }
        });

        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
